package com.returnsoft.softphone.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.softphone.dto.CampaignDto;
import com.returnsoft.softphone.eao.CampaignEao;
import com.returnsoft.softphone.entity.Campaign;
import com.returnsoft.softphone.exception.ConversionExcepcion;
import com.returnsoft.softphone.exception.CCenterExcepcion;
import com.returnsoft.softphone.exception.EaoExcepcion;
import com.returnsoft.softphone.util.Conversion;

/**
 * Session Bean implementation class CampaignBean
 */
@Stateless
@LocalBean
public class CampaignBean implements CampaignInterface, Serializable {
	private static final long serialVersionUID = 1L;
	@EJB
	private CampaignEao eao;
	@EJB
	private Conversion conv;

	/*@Override
	public List<CampaignDto> findAll() throws CustomExcepcion {
		try {
			List<Campaign> campaigns = eao.findAll();
			List<CampaignDto> campaignsDto = new ArrayList<CampaignDto>();
			for (Campaign campaign : campaigns) {
				CampaignDto campaignDto = conv.fromCampaign(campaign);
				campaignsDto.add(campaignDto);
			}
			return campaignsDto;
		} catch (Exception e) {
			throw new CustomExcepcion(e.getMessage());
		}
	}*/

	@Override
	public List<CampaignDto> findByServer(Integer serverId)
			throws CCenterExcepcion {
		try {
			List<Campaign> campaigns = eao.findByServer(serverId);
			List<CampaignDto> campaignsDto = new ArrayList<CampaignDto>();
			for (Campaign campaign : campaigns) {
				CampaignDto campaignDto = conv.fromCampaign(campaign);
				campaignsDto.add(campaignDto);
			}
			return campaignsDto;
		} catch (EaoExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new CCenterExcepcion(e.getMessage());
		}
	}

	@Override
	public List<CampaignDto> findByAgent(Integer agentId)
			throws CCenterExcepcion {
		try {
			List<Campaign> campaigns = eao.findByAgent(agentId);
			List<CampaignDto> campaignsDto = new ArrayList<CampaignDto>();
			for (Campaign campaign : campaigns) {
				CampaignDto campaignDto = conv.fromCampaign(campaign);
				campaignsDto.add(campaignDto);
			}
			return campaignsDto;
		} catch (EaoExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new CCenterExcepcion(e.getMessage());
		}
	}
	
	@Override
	public List<CampaignDto> findSubCampaigns(Integer campaignId)
			throws CCenterExcepcion {
		try {
			List<Campaign> campaigns = eao.findByCampaignId(campaignId);
			List<CampaignDto> campaignsDto = new ArrayList<CampaignDto>();
			for (Campaign campaign : campaigns) {
				CampaignDto campaignDto = conv.fromCampaign(campaign);
				campaignsDto.add(campaignDto);
			}
			return campaignsDto;
		} catch (EaoExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new CCenterExcepcion(e.getMessage());
		}
	}
	

}
